package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.BookDetailsDAO;
import DAO.DAOFactory;
import DAO.NotFoundException;
import DAO.UserDAO;
import model.BookDetailsDTO;
import model.UserDTO;

/**
 * Servlet implementation class ReviewController
 */
@WebServlet("/ReviewController")
public class ReviewController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ReviewController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest req, HttpServletResponse res){
		ArrayList<BookDetailsDTO> table;
		ArrayList<UserDTO> users = new ArrayList<UserDTO>();
		UserDTO user;
		BookDetailsDAO b = DAOFactory.loadInstance().getBookDetailsDAO();
		UserDAO d = DAOFactory.loadInstance().getUserDAO();
		
		try{
			table = b.findAwaitingApproval();
			//get booking person for each book detail
			for(BookDetailsDTO booking : table){
				user = d.findbyId(booking.getBookedBy());
				users.add(user);
			}
			
			req.setAttribute("bookdetails", table);
			req.setAttribute("users", users);
			
		} catch (NotFoundException e) {
			e.printStackTrace();
		}

		RequestDispatcher rd = req.getRequestDispatcher("/ReviewBooking.jsp");
		try {
			rd.forward(req, res);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
}

}
